package com.qf.gateway.listener;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * @author 30909
 *  1、在nacos 上获取配置信息，且监听 nacos 上的配置信息
 *  2、拿到配置信息后，让网关应用的路由器在线动态生效
 */
@Component
public class RouteInfoListener implements ApplicationEventPublisherAware {

    /**
     * 启动动作发布zhe
     */
    private ApplicationEventPublisher applicationEventPublisher;
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher=applicationEventPublisher;
    }


    /**
     * nacos 上配置文件对应的 dataID
     */
    private String dataId = "gateway-router";
    /**
     * nacos 上的配置文件对应的 group
     */
    private String group = "DEFAULT_GROUP";

    /**
     *  nacos 的服务器地址
     */
    @Value("&{spring.cloud.nacos.config.server-addr}")
    private String serverAddr;

    /**
     * 保存路由信息
     */
    private static final List<String> ROUTE_LIST = new ArrayList<>();

    /**
     * 路由写入对象
     */
    @Resource
    private RouteDefinitionWriter routeDefinitionWriter;

    /**
     * 路由信息写如对象
     */

    public void  clearRoute(){
        for (String id:ROUTE_LIST){
            //逐个删除
            this.routeDefinitionWriter.delete(Mono.just(id)).subscribe();
        }
        ROUTE_LIST.clear();
    }
    /**
     * 添加路由信息的方法
     */


    /**
     * 发布路由信息的方法
     */








    @PostConstruct
    public  void dynamicRouteByNacosListener(){
        try {
            //获取nacos 服务
            ConfigService configService = NacosFactory.createConfigService(serverAddr);
            //获取路由信息
            String config = configService.getConfig(dataId, group, 5000);
//            System.out.println("config = " + config);

            List<RouteDefinition> routeDefinitions = JSONObject.parseArray(config, RouteDefinition.class);



            //监听逻辑
            configService.addListener(dataId, group, new Listener() {
                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String config) {
                    System.out.println("config = " + config);
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
